﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlimDX;
using Gype.UI;

namespace Gype.GUI.Components
{
    internal class ContactPopup
    {
        internal ContactPopup()
        {
            UserFriendlyName = "";
        }

        internal void Render(Vector2 position, bool left)
        {
            if (mTarget == null)
                return;

            if (left)
                renderLeft(position);
            else
                renderRight(position);
        }

        private void renderLeft(Vector2 position)
        {
            mTarget.FillTriangle(position, new Vector2(position.X - 20, position.Y - 20), new Vector2(position.X - 40, position.Y - 20), BrushCollection.ContactPopupBorder);
        }

        private void renderRight(Vector2 position)
        {
            mTarget.FillRoundedRectangle(new Vector2(position.X - 29, position.Y - 149), new Vector2(298, 128), 4, 4, BrushCollection.ContactPopupBackground);
            mTarget.DrawRoundedRectangle(new Vector2(position.X - 30, position.Y - 150), new Vector2(300, 130), 4, 4, BrushCollection.ContactPopupBorder, 3);
            mTarget.FillTriangle(position, new Vector2(position.X + 20, position.Y - 22), new Vector2(position.X + 40, position.Y - 22), BrushCollection.ContactPopupBorder);

            lock (LockObject)
            {
                if (UserAvatar != null)
                    mTarget.DrawImage(UserAvatar, new System.Drawing.RectangleF(position.X - 20, position.Y - 140, 64, 64));
            }

            if (string.IsNullOrEmpty(UserFriendlyName) == false)
            {
                mInfoText.Color = BrushCollection.TabHeaderTextSelect;
                mInfoText.DrawText(UserFriendlyName, new Vector2(position.X - 20 + 64 + 5, position.Y - 140), new Vector2(215, 14));
            }

            if (User != null)
            {
                mInfoText.Color = BrushCollection.TabHeaderTextSelect;
                if (string.IsNullOrEmpty(User.Country) == false)
                    mInfoText.DrawText(User.Country, new Vector2(position.X - 20 + 64 + 5, position.Y - 125), new Vector2(215, 14));

                mInfoText.DrawText(User.Birthday.ToShortDateString(), new Vector2(position.X - 20 + 64 + 5, position.Y - 110), new Vector2(215, 14));
                if (string.IsNullOrEmpty(User.MoodText) == false)
                {
                    mInfoText.SetMultiLine(13);
                    mInfoText.ParagraphAlignement = ParagraphAlignement.Top;
                    mInfoText.DrawText(User.MoodText, new Vector2(position.X - 20, position.Y - 140 + 70), new Vector2(215 + 69, 50));
                    mInfoText.ParagraphAlignement = ParagraphAlignement.Center;
                    mInfoText.SetSingleLine();
                }

                Image onlineImage = ImageCollection.OnlineImageList[SKYPE4COMLib.TOnlineStatus.olsUnknown];
                if (ImageCollection.OnlineImageList.ContainsKey(User.OnlineStatus))
                    onlineImage = ImageCollection.OnlineImageList[User.OnlineStatus];

                mTarget.DrawImage(onlineImage, new System.Drawing.RectangleF(position.X - 20 + 64 + 5, position.Y - 94, 16, 16));
                mInfoText.DrawText(getOnlineString(User.OnlineStatus), new Vector2(position.X - 20 + 64 + 5 + 20, position.Y - 92), new Vector2(215, 14));
            }
        }

        private string getOnlineString(SKYPE4COMLib.TOnlineStatus status)
        {
            return (status.ToString()).Substring(3);
        }

        internal void attachToTarget(Rendertarget target)
        {
            mTarget = target;
            mInfoText = new TextElement(TextCollection.Arial);
            mInfoText.Alignment = TextAlignement.Left;
        }

        Rendertarget mTarget = null;
        TextElement mInfoText;

        internal SKYPE4COMLib.User User { get; set; }
        internal string UserFriendlyName { get; set; }
        internal Image UserAvatar { get; set; }
        internal object LockObject = new object();
    }
}
